package Project;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import Gameplay.Level;
/**
 * This class is to draw all the graphics for 
 * the game's galaxy map, and controls for next
 * world or previous world.
 * 
 * @author	Brian Wetherbee
 * 			Ryan McNamara
 * 			Zeng Yang
 *
 *
 */
public class GalaxyMap extends Menu {
	/*@ public invariant menuPosition <= levelsBeaten; @*/
	/*@ public invariant Main.UI = this; @*/
	
	/**
     * Creates new form CustomizationMenu
     */
    public GalaxyMap() {
        this(0);
    }
    
    public GalaxyMap(int opt) {
    	super(2 * Constants.NUM_WORLDS - 1);
    	updateInformation();
    	
    	initComponents();
    	this.setOption(opt);
    }
    
    @Override
    public void paint(java.awt.Graphics g)
    {
    	super.paint(g);

    	g.setColor(Color.BLACK);
    	g.fillRect(0,0,1672,968);
    	
    	//Draw UI
    	g.setColor(new Color(64,64,64));
    	g.fillRect(0, 0, 1672, 64);
    	g.fillRect(0, 904, 1672, 64);
    	g.setColor(new Color(128,128,128));
    	g.drawRect(0, 0, 1672, 64);
    	g.drawRect(0, 904, 836, 64);
    	g.drawRect(836, 904, 836, 64);
    	
    	g.setColor(Color.WHITE);
    	g.setFont(new java.awt.Font("DialogInput",1,48));
    	FontMetrics f = g.getFontMetrics(new java.awt.Font("DialogInput",1,48));
    	double stringX = Main.actualToInternalX(f.stringWidth("Galaxy Map"));
    	double stringY = Main.actualToInternalY(f.getHeight());
    	g.drawString("Galaxy Map", (int)(Constants.width / 2 - (stringX/2)), (int)(48 - ((64 - stringY)/2)));
    	
    
    	//Draw Connectors
    	g.setColor(Color.WHITE);
    	
    	g.drawLine(167, 212, 460, 733);
        g.drawLine(460, 733, 711, 451);
        g.drawLine(711, 451, 1087, 213);
        g.drawLine(1087, 213, 1254, 596);
        g.drawLine(1254, 596, 1463, 752);
        g.drawLine(1463, 752, 1628, 433);
        
    	//Draw Planets
        g.setColor(new Color(127,0,0));
        g.fillOval(84, 129, 167, 167);
        
        g.setColor(new Color(255,106,0));
        g.fillOval(334, 607, 251, 251);
        
        g.setColor(new Color(127,51,0));
        g.fillOval(585, 325, 251, 251);
        
        g.setColor(new Color(0,255,255));
        g.fillOval(1003, 129, 167, 167);
        
        g.setColor(new Color(0,148,255));
        g.fillOval(1170, 512, 167, 167);
        
        g.setColor(new Color(128,128,128));
        g.fillOval(1421, 710, 84, 84);
        
        g.setColor(new Color(255,0,0));
        g.fillOval(1461, 266, 334, 334);
        
        //Draw ITLs
        g.setColor(new Color(64,64,64));
        g.fillOval(261, 397, 63, 63);
        g.fillOval(559, 552, 63, 63);
        g.fillOval(896, 284, 63, 63);
        g.fillOval(1139, 379, 63, 63);
        g.fillOval(1343, 659, 63, 63);
        g.fillOval(1492, 611, 63, 63);
        
        drawSelector(g);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
        addKeyListener(this);
        addMouseListener(this);
        repaint();
    }// </editor-fold>                

    // Variables declaration - do not modify
    // End of variables declaration                   
    private int worldsUnlocked;
    
    @Override
    public void select() {
    	//this is a test to see if it is a world
    	if(getOption() % 2 == 0){
    		//it's a world map
    		UI worldMap = new LocalMap(getOption() / 2);
    		Main.setCurrentUI(worldMap);
    	}
    	else{
    		//it's an IT level
    		int levelNo = (getOption() + 1) / 2 * (Constants.NUM_LEVELS_PER_WORLD) + (getOption() + 1) / 2 - 1;
    		UI toAdd = new Level(levelNo,this,true);
    		Main.setCurrentUI(toAdd);
    		((Level)toAdd).startLevel();
    	}
    	
    }
    
    @Override
    public void increaseHighlight()
    {
        setOption((getOption() + 1) % (worldsUnlocked + 1));
        repaint();
    }
    
    @Override
    public void decreaseHighlight()
    {
        setOption((getOption() - 1) % (worldsUnlocked + 1));
        if(getOption() == -1)
        {
        	setOption(worldsUnlocked);
        }
        repaint();
    }
    
    @Override
    public void updateInformation(){
    	worldsUnlocked = (SaveFile.getCurrentFile().getLevelsBeaten() / (Constants.NUM_LEVELS_PER_WORLD + 1)) * 2;
    	if(SaveFile.getCurrentFile().getLevelsBeaten() % (Constants.NUM_LEVELS_PER_WORLD + 1) == Constants.NUM_LEVELS_PER_WORLD)
        {
        	worldsUnlocked += 1;
        }
    }
    
    private void drawSelector(Graphics g)
    {
    	g.setColor(new Color(64,64,64));
    	g.fillRect(0, 904, 836, 64);
    	g.setColor(new Color(128,128,128));
    	g.drawRect(0, 904, 836, 64);
    	
    	FontMetrics f = g.getFontMetrics(new Font("DialogInput",1,48));
    	int nameX = 0;
    	int nameY = 0;
        
    	g.setColor(new Color(224,224,224));
        if(getOption() == 0) {
        	g.drawOval(84, 129, 167, 167);
        	nameX = f.stringWidth("Mars");
        	nameY = f.getHeight();
        	g.setColor(Color.WHITE);
        	g.drawString("Mars", 418 - (nameX / 2), 952 - ((64 - nameY) / 2));
        } else if(getOption() == 1) {
        	g.drawOval(261, 397, 63, 63);
        	nameX = f.stringWidth("To Jupiter");
        	nameY = f.getHeight();
        	g.setColor(Color.WHITE);
        	g.drawString("To Jupiter", 418 - (nameX / 2), 952 - ((64 - nameY) / 2));
        } else if(getOption() == 2) {
        	g.drawOval(334, 607, 251, 251);
        	nameX = f.stringWidth("Jupiter");
        	nameY = f.getHeight();
        	g.setColor(Color.WHITE);
        	g.drawString("Jupiter", 418 - (nameX / 2), 952 - ((64 - nameY) / 2));
        } else if(getOption() == 3) {
        	g.drawOval(559, 552, 63, 63);
        	nameX = f.stringWidth("To Saturn");
        	nameY = f.getHeight();
        	g.setColor(Color.WHITE);
        	g.drawString("To Saturn", 418 - (nameX / 2), 952 - ((64 - nameY) / 2));
        } else if(getOption() == 4) {
        	g.drawOval(585, 325, 251, 251);
        	nameX = f.stringWidth("Saturn");
        	nameY = f.getHeight();
        	g.setColor(Color.WHITE);
        	g.drawString("Saturn", 418 - (nameX / 2), 952 - ((64 - nameY) / 2));
        } else if(getOption() == 5) {
        	g.drawOval(896, 284, 63, 63);
        	nameX = f.stringWidth("To Uranus");
        	nameY = f.getHeight();
        	g.setColor(Color.WHITE);
        	g.drawString("To Uranus", 418 - (nameX / 2), 952 - ((64 - nameY) / 2));
        } else if(getOption() == 6) {
        	g.drawOval(1003, 129, 167, 167);
        	nameX = f.stringWidth("Uranus");
        	nameY = f.getHeight();
        	g.setColor(Color.WHITE);
        	g.drawString("Uranus", 418 - (nameX / 2), 952 - ((64 - nameY) / 2));
        } else if(getOption() == 7) {
        	g.drawOval(1139, 379, 63, 63);
        	nameX = f.stringWidth("To Neptune");
        	nameY = f.getHeight();
        	g.setColor(Color.WHITE);
        	g.drawString("To Neptune", 418 - (nameX / 2), 952 - ((64 - nameY) / 2));
        } else if(getOption() == 8) {
        	g.drawOval(1170, 512, 167, 167);
        	nameX = f.stringWidth("Neptune");
        	nameY = f.getHeight();
        	g.setColor(Color.WHITE);
        	g.drawString("Neptune", 418 - (nameX / 2), 952 - ((64 - nameY) / 2));
        } else if(getOption() == 9) {
        	g.drawOval(1343, 659, 63, 63);
        	nameX = f.stringWidth("To Pluto");
        	nameY = f.getHeight();
        	g.setColor(Color.WHITE);
        	g.drawString("To Pluto", 418 - (nameX / 2), 952 - ((64 - nameY) / 2));
        } else if(getOption() == 10) {
        	g.drawOval(1421, 710, 84, 84);
        	nameX = f.stringWidth("Pluto");
        	nameY = f.getHeight();
        	g.setColor(Color.WHITE);
        	g.drawString("Pluto", 418 - (nameX / 2), 952 - ((64 - nameY) / 2));
        } else if(getOption() == 11) {
        	g.drawOval(1492, 611, 63, 63);
        	nameX = f.stringWidth("To Mayacta");
        	nameY = f.getHeight();
        	g.setColor(Color.WHITE);
        	g.drawString("To Mayacta", 418 - (nameX / 2), 952 - ((64 - nameY) / 2));
        } else if(getOption() == 12) {
        	g.drawOval(1461, 266, 334, 334);
        	nameX = f.stringWidth("Mayacta");
        	nameY = f.getHeight();
        	g.setColor(Color.WHITE);
        	g.drawString("Mayacta", 418 - (nameX / 2), 952 - ((64 - nameY) / 2));
        }
    }

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == Constants.controlLeft)
        {
            decreaseHighlight();
        }
        if(e.getKeyCode() == Constants.controlRight)
        {
            increaseHighlight();
        }
        if(e.getKeyCode() == Constants.controlConfirm)
        {
            select();
        }
        if(e.getKeyCode() == Constants.controlCancel)
        {
        	SaveFile.SaveCurrentFile();
        	SaveFile.UnLoadCurrentFile();
        	Main.setCurrentUI(new TitleScreen());
        }
        if(e.getKeyCode() == Constants.controlQuit)
        {
        	System.exit(0);
        }
        if(e.getKeyCode() == Constants.controlCustomize)
        {
        	Main.setCurrentUI(new CustomizationMenu(this));
        }
        if(e.getKeyCode() == Constants.controlUpgrade)
        {
        	Main.setCurrentUI(new UpgradeMenu(this));
        }
        if(e.getKeyCode() == KeyEvent.VK_T)
        {
        	worldsUnlocked = 6;
        	SaveFile.getCurrentFile().setLevelsBeaten(12);
        }
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
}
